from check_log_grab.core.zk_teco import ZkTecoReptile
from check_log_grab.core.wechat import WechatReptile
from check_log_grab.rep_config import ReptileConfig
from check_log_grab.common import date_grouping, dates_timestamp
from loguru import logger as log
from utils.date_tools import timestamp_to_datetime


class CheckLogReptile(object):
    """

    """

    def __init__(self, fp):
        """

        :param fp:
        """
        self.T = []
        self.result = {}
        self.clg = ReptileConfig(fp).loads()

    def load_check_in_data(self, dates, users: dict, out_file=False, fmt="%Y-%m-%d"):
        """
        加载考勤终端上面的数据，需要指定考勤的日期段
        :param dates:
        :param fmt:
        :param users:
        :param out_file:
        :return:
        """
        if self.clg["ZK_FLAG"]:
            start_date, end_date = dates_timestamp(dates, fmt)
            for zk_c in self.clg.get("ZK", []):
                with ZkTecoReptile(zk_c)as zk:
                    self.T.extend(
                        zk.get_check_in_data(users.get("zk", []), start=start_date, end=end_date, out_file=out_file))
        if self.clg["WX_FLAG"]:
            task_date = date_grouping(dates)
            if len(users) <= 99:
                uid_arr = [users.get("wx", [])]
            else:
                u_ids = users.get("wx", [])
                uid_arr = [u_ids[i:i + 99] for i in range(0, len(u_ids), 99)]
            with WechatReptile(self.clg.get("WX", {}))as wx:
                for date_arr in task_date:
                    start_date, end_date = dates_timestamp(date_arr, fmt)
                    for sub_u_ids in uid_arr:
                        self.T.extend(wx.get_check_in_data(start_date, end_date, sub_u_ids, out_file=out_file))
        log.info(f"共计：{len(self.T)} 条数据")

    def duplicate_removal(self):
        log.info("开始检测重复数据。。。。。")

        for cd in self.T:
            d_id = f"{cd['userid']}-{cd['checkin_time']}"
            if self.result.get(d_id, None) is None:
                self.result[d_id] = cd
                continue

    def get_check_in_data(self):
        """
        获取
        :return:[{
                "userid": id_num,
                "checkin_time": int,
                "groupname": ,
                "checkin_type": '',
                "location_title": title,
                "location_detail": detail,
                "deviceid": ,
        }]
        """
        self.duplicate_removal()
        log.info(f"去重后：{len(self.result)} 条数据")
        return list(self.result.values())


if __name__ == '__main__':
    uids = ['wxiaoying', '7328559358526966639', 'f-459840191668798737', '5754556578835418313', 'f-280507862050555354',
            '6651527592048738362', 'f-376633407229576512', 'f-666600841407789276', '9065387585141949721', 'sjiayu',
            'f-337864415953072595', '3903891829773453594', 'f-471315359745964062', 'f-819937145389912689',
            'f-752977425499872956', '3284398533973461013', '8606954318644595828', '5485374180279533654',
            'f-299029897120990793', '3178625498445138187', 'f-427906875918651913', '8104123663563702420',
            'f-160822540244534615', 'f-609145241764821079', '7923737386780280618', '2894201846087042192',
            'f-811226589340032997', '3019559123002740004', 'f-730441521150045267', 'f-412461861368546855',
            '6403213505242549968', '2298919643427979934', 'f-438928340594764714', 'f-634458889098695369']
    dt = ['2022-04-05', '2022-04-06', '2022-04-07', '2022-04-08', '2022-04-09', '2022-04-10', '2022-04-11',
          '2022-04-12',
          '2022-04-13', '2022-04-14', '2022-04-15', '2022-04-16', '2022-04-17', '2022-04-18']

    clr = CheckLogReptile(r"E:\SrcCode\python\attendance_data2\reptile_con.json")
    clr.load_check_in_data(dt)
    print(clr.get_check_in_data())
